﻿using ServiceStack.ServiceClient.Web;
using ServiceStack.Logging;
using g42.Dto;

namespace g42.ServiceConsumer
{
  public class ServCons
  {
    private ILog log = null;
    private readonly string ListeningOn = ServiceStack.Configuration.ConfigUtils.GetAppSetting("ListeningOn");
    public JsonServiceClient ServiceClient = null;

    public void Start()
    {
      log = LogManager.GetLogger(GetType());
      ServiceClient = new JsonServiceClient(ListeningOn);
      log.Info("client is ready (looking for " + ListeningOn + ")");
    }

    public void CreateTestData()
    {
      if (ServiceClient == null)
      {
        Start();
      }
      log.Info("start creating testdata");
      Mandant md;
      Strasse str;
      Adresse adr;
      Person p;
      md = ServiceClient.Post(new Mandant { Nr = "1", Name = "default" });
      str = ServiceClient.Post(new Strasse { Nr = "1", Name = "Hauptstr.", Laenge = 1800 });
      adr = ServiceClient.Post(new Adresse { Nr = "1", Strasse = str, HauseNr1 = 42 });
      p = ServiceClient.Post(new Person { Nr = "1", Md = md, Adresse = adr, Vorname = "Fritzchen", Name = "Testmeier" } );
      adr = ServiceClient.Post(new Adresse { Nr = "2", Strasse = str, HauseNr1 = 43 });
      p = ServiceClient.Post(new Person { Nr = "2", Md = md, Adresse = adr, Vorname = "Karl", Name = "May" });
      str = ServiceClient.Post(new Strasse { Nr = "2", Name = "Wäschenweg", Laenge = 7700 });
      adr = ServiceClient.Post(new Adresse { Nr = "3", Strasse = str, HauseNr1 = 12 });
      p = ServiceClient.Post(new Person { Nr = "3", Md = md, Adresse = adr, Vorname = "Sepp", Name = "Maier" });
      p = ServiceClient.Post(new Person { Nr = "4", Md = md, Adresse = adr, Vorname = "Hansi", Name = "Müller" });
      p = ServiceClient.Post(new Person { Nr = "5", Md = md, Adresse = adr, Vorname = "Paul", Name = "Breitner" });
      log.Info("end creating testdata");
    }

  }
}
